<?php
require_once("../resources/commons.php");
require_once("../data/User.php");
require_once("../data/Activity.php");
require_once("admin_commons.php");
checkAdminRights();

/**
 * Display the activity edition form
 * @param Activity $p_record
 */
function updateActivity($p_record){
	?>
	<a href="update_activity.php?updateId=<?=base64_encode($p_record->getId()) ?>">Display</a>
	<?php
}

displayTop("Manage activities","../resources/ajaxscripts.php",null);
?>
<p>Defined activities:</p>
<?php

/*
Get the parameters passed in the page query string (sorting criteria)
Two parameters are supported:
1) titleSort: sort by TITLE DESC if equal to 1 (ORDER ASC otherwise)
2) activityTypeSort: sort by TITLE DESC if equal to 1 (ORDER ASC otherwise)
*/
define("TITLE_SORT_REQUEST_PARAMETER","titleSort");
define("ACTIVITY_TYPE_SORT_REQUEST_PARAMETER","activityTypeSort");
define("ACTIVITY_STATUS_SORT_REQUEST_PARAMETER","activityStatus");

$l_sortStatement="ORDER BY title DESC";
$l_titleHeaderLabel="<a href=\"?" . TITLE_SORT_REQUEST_PARAMETER . "=0\">Title</a>";
$l_activityTypeHeaderLabel="<a href=\"?" . ACTIVITY_TYPE_SORT_REQUEST_PARAMETER . "=0\">Type</a>";
$l_activityStatusHeaderLabel="<a href=\"?" . ACTIVITY_STATUS_SORT_REQUEST_PARAMETER . "=0\">Status</a>";

if($_REQUEST[TITLE_SORT_REQUEST_PARAMETER]=="1"){
	$l_titleHeaderLabel="<a href=\"?" . TITLE_SORT_REQUEST_PARAMETER . "=0\">Title</a>";
	$l_sortStatement="ORDER BY title DESC";
}elseif($_REQUEST[TITLE_SORT_REQUEST_PARAMETER]=="0"){
	$l_titleHeaderLabel="<a href=\"?" . TITLE_SORT_REQUEST_PARAMETER . "=1\">Title</a>";
	$l_sortStatement="ORDER BY title ASC";
}
if($_REQUEST[ACTIVITY_TYPE_SORT_REQUEST_PARAMETER]=="1"){
	$l_activityTypeHeaderLabel="<a href=\"?" . ACTIVITY_TYPE_SORT_REQUEST_PARAMETER . "=0\">Type</a>";
	$l_sortStatement="ORDER BY activityType_id DESC";
}elseif($_REQUEST[ACTIVITY_TYPE_SORT_REQUEST_PARAMETER]=="0"){
	$l_activityTypeHeaderLabel="<a href=\"?" . ACTIVITY_TYPE_SORT_REQUEST_PARAMETER . "=1\">Type</a>";
	$l_sortStatement="ORDER BY activityType_id ASC";
}
if($_REQUEST[ACTIVITY_STATUS_SORT_REQUEST_PARAMETER]=="1"){
	$l_activityStatusHeaderLabel="<a href=\"?" . ACTIVITY_STATUS_SORT_REQUEST_PARAMETER . "=0\">Status</a>";
	$l_sortStatement="ORDER BY isArchived DESC";
}elseif($_REQUEST[ACTIVITY_STATUS_SORT_REQUEST_PARAMETER]=="0"){
	$l_activityStatusHeaderLabel="<a href=\"?" . ACTIVITY_STATUS_SORT_REQUEST_PARAMETER . "=1\">Status</a>";
	$l_sortStatement="ORDER BY isArchived ASC";
}

$l_records=Activity::getFromCustomCriteria($l_sortStatement,array());
$l_activitiesTable=new TableData("ActivitiesTable");
$l_activitiesTable->addHeader("title",$l_titleHeaderLabel);
$l_activitiesTable->addHeader("activityTypeLabel",$l_activityTypeHeaderLabel);
$l_activitiesTable->addHeader("isArchivedStatus",$l_activityStatusHeaderLabel);
$l_activitiesTable->addAction("updateActivity");
$l_activitiesTable->setRecords($l_records);
$l_activitiesTable->setZoomArea("ajax_activity_details.php");

displayTable($l_activitiesTable);

displayBottom();
?>